class ProductusersController < ApplicationController
  # GET /productusers
  # GET /productusers.xml
  def index
    @title='分类类别'
    @productusers = Productuser.find(:all)
    

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @productusers }
    end
  end

  # GET /productusers/1
  # GET /productusers/1.xml
  def show
    @productuser = Productuser.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @productuser }
    end
  end

  # GET /productusers/new
  # GET /productusers/new.xml
  def new
    @productuser = Productuser.new

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @productuser }
    end
  end

  # GET /productusers/1/edit
  def edit
    @productuser = Productuser.find(params[:id])
  end

  # POST /productusers
  # POST /productusers.xml
  def create
    @productuser = Productuser.new(params[:productuser])

    respond_to do |format|
      if @productuser.total_cost > 0 
          @productuser.total_cost = @productuser.per_cost * @productuser.amount 
          @productuser.save
      #  flash[:notice] = 'Productuser was successfully created.'
        format.html { redirect_to(:controller => "users",:action => "add_products",:id => params[:productuser][:user_id]) }
        format.xml  { render :xml => @productuser, :status => :created, :location => @productuser }
      else
        flash[:notice] = '领取失败,总花费不能为零.'
        format.html { redirect_to(:controller => "users",:action => "add_products",:id => params[:productuser][:user_id]) }
        format.xml  { render :xml => @productuser.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /productusers/1
  # PUT /productusers/1.xml
  def update
    @productuser = Productuser.find(params[:id])

    respond_to do |format|
      if @productuser.update_attributes(params[:productuser])
        flash[:notice] = 'Productuser was successfully updated.'
        format.html { redirect_to("/users/#{@productuser.user.id}") }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @productuser.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /productusers/1
  # DELETE /productusers/1.xml
  def destroy
    @productuser = Productuser.find(params[:id])
    @productuser.destroy

    respond_to do |format|
      format.html { redirect_to(productusers_url) }
      format.xml  { head :ok }
    end
  end
end
